﻿using ASPNETVN.PORTAL.Components.UI;
using System;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Modules.CONTACT.Components;
using System.Collections;

namespace ASPNETVN.PORTAL.Modules.CONTACT.Controls
{
    public partial class AddEditContact : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            ButtonCancel = this.btnCancel;
            ButtonUpdate = this.btnUpdate;
            ButtonDelete = this.btnDelete;
            this.Update += new EventHandler(AddEditContact_Update);
            this.Delete += new EventHandler(AddEditContact_Delete);
            base.OnInit(e);
        }

        void AddEditContact_Delete(object sender, EventArgs e)
        {
            if (this.ItemID != 0)
            {
                Item.DeleteItem(this.ItemID);
            }
            this.ReturnToReferrerPage();
        }

        void AddEditContact_Update(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Item contact = Item.Load(this.ItemID);
                if (contact == null)
                {
                    contact = new Item();
                }

                contact.ModuleID = this.ModuleID;
                contact.FullName = txtName.Text;
                contact.Role = txtRole.Text;
                contact.Email = txtEmail.Text;
                contact.Contact1 = txtContact1.Text;
                contact.Contact2 = txtContact2.Text;
                contact.Contact3 = txtContact3.Text;
                contact.Contact4 = txtContact4.Text;

                if (this.ItemID == 0)
                {
                    contact.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                    contact.CreatedDate = DateTime.Now;
                    contact.Insert();
                }
                else
                {
                    contact.ID = this.ItemID;
                    contact.Update();
                }

                this.ReturnToReferrerPage();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                Hashtable settings = this.Settings;
                lblContact1.Text = settings["Contact1"].ToString();
                lblContact2.Text = settings["Contact2"].ToString();
                lblContact3.Text = settings["Contact3"].ToString();
                lblContact4.Text = settings["Contact4"].ToString();

                if (this.ItemID > 0)
                {
                    Item contact = Item.Load(this.ItemID);
                    if (contact != null)
                    {
                        txtName.Text = contact.FullName;
                        txtRole.Text = contact.Role;
                        txtEmail.Text = contact.Email;
                        txtContact1.Text = contact.Contact1;
                        txtContact2.Text = contact.Contact2;
                        txtContact3.Text = contact.Contact3;
                        txtContact4.Text = contact.Contact4;
                        txtCreatedBy.Text = contact.CreatedBy;
                        txtCreatedDate.Text = contact.CreatedDate.ToString();
                    }
                }
            }
        }
    }
}